/**
 * @file      state_manager.c
 * @brief
 * @version   1.0
 * @date      2023.12.14
 *
 * @copyright Copyright (c) 2018 UCchip Technology Co.,Ltd. All rights reserved.
 *
 * Change Logs:
 * Date          Author       Notes
 * 2023.12.14    ucchip       the first version
 *
 */
#include "state_manager.h"
#include "led_manager.h"
#include <rtthread.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME "state"
#include "uc_log.h"

// 网关状态全局变量
static manager_state_t g_manager_state = {0};

/**
 * @brief 获取MQTT状态
 *
 * @return mqtt_state_e MQTT状态
 */
mqtt_state_e manager_get_mqtt_state(void)
{
    return g_manager_state.mqtt_state;
}

/**
 * @brief 设置MQTT状态
 *
 * @param mqtt_state MQTT状态
 */
void manager_set_mqtt_state(mqtt_state_e mqtt_state)
{
    // 打印状态
    TRACE_I("mqtt %s", mqtt_state ? "online" : "offline");
    TRACE_I("mqtt_state %d to %d", g_manager_state.mqtt_state, mqtt_state);
    if (MQTT_STATE_ONLINE == mqtt_state)
    {
        send_led_event(LED_MQTT, LED_E_NORMAL);
    }
    else
    {
        send_led_event(LED_MQTT, LED_E_EXCEPTION);
    }
    g_manager_state.mqtt_state = mqtt_state;
}

/**
 * @brief 获取WIoTa状态
 *
 * @return wiota_state_e WIoTa状态
 */
wiota_state_e manager_get_wiota_state(void)
{
    return g_manager_state.wiota_state;
}

/**
 * @brief 设置WIoTa状态
 *
 * @param wiota_state WIoTa状态
 */
void manager_set_wiota_state(wiota_state_e wiota_state)
{
    TRACE_I("wiota_state %d to %d", g_manager_state.wiota_state, wiota_state);
    if (WIOTA_OFFLINE == wiota_state)
    {
        send_led_event(LED_WIOTA, LED_E_EXCEPTION);
    }
    else
    {
        send_led_event(LED_WIOTA, LED_E_NORMAL);
    }
    g_manager_state.wiota_state = wiota_state;
}

/**
 * @brief 获取启动信息上报状态
 *
 * @return get_info_state_e 启动信息上报状态
 */
get_info_state_e manager_get_startup_info_state(void)
{
    return g_manager_state.get_info_state;
}

/**
 * @brief 设置启动信息上报状态
 *
 * @param info_state 启动信息上报状态
 */
void manager_set_startup_info_state(get_info_state_e info_state)
{
    TRACE_I("info_state %d to %d", g_manager_state.get_info_state, info_state);
    g_manager_state.get_info_state = info_state;
}

/**
 * @brief  获取userid管理模块初始化状态
 *
 * @return userid_manager_state_e serid管理模块初始化状态
 */
userid_manager_state_e manager_get_userid_manager_state(void)
{
    return g_manager_state.userid_state;
}

/**
 * @brief 设置userid管理模块初始化状态
 *
 * @param userid_state userid管理模块初始化状态
 */
void manager_set_userid_manager_state(userid_manager_state_e userid_state)
{
    TRACE_I("userid_state %d to %d", g_manager_state.userid_state, userid_state);
    g_manager_state.userid_state = userid_state;
}

/**
 * @brief 获取扫频状态
 *
 * @return int 1表示正常扫频，0表示未在扫频
 */
int manager_get_scan_freq_state(void)
{
    return g_manager_state.scan_freq_state;
}

/**
 * @brief 设置扫频状态
 *
 * @param state 1表示正常扫频，0表示未在扫频
 */
void manager_set_scan_freq_state(int state)
{
    g_manager_state.scan_freq_state = state;
}
